using System;
using System.Collections.Generic;
using System.Text;

namespace Slobby.Server {
    public class Client {
        public readonly string Name;
        public System.Net.IPAddress Address {
            get {
                return conn.Address;
            }
        }

        private Net.ClientConnection conn;

        public event Action<object> RecieveMessage;

        public Client(Net.ClientConnection connection, string name) {
            Name = name;
            conn = connection;
        }

        public void StartListening() {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ListenFunction));
        }

        private void ListenFunction(object state) {
            object o = null;

            try {
                o = conn.RecieveMessage();
            }
            catch (Exception) {
                conn.Disconnect();
                return;
            }
            RecieveMessage(o);

            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ListenFunction));
        }

        public void SendMessage(object message) {
            conn.SendMessage(message);
        }
    }
}
